﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.Text;
using WebApiClient;

namespace ARchGL.Platform.Qianbian
{
    /// <summary>
    /// 千变接口
    /// </summary>
    public static class QianbianHttpsModule
    {
        public static IServiceCollection Register(IServiceCollection services, IConfigurationRoot appConfiguration)
        {
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            void config(HttpApiConfig c)
            {
                c.HttpHost = new Uri(appConfiguration["Qianbian:Url"]);
                c.GlobalFilters.Add(new DefaultHeaderAttribute(appConfiguration));
                //c.GlobalFilters.Add(new ServiceTokenAttribute());
            }

            services.AddHttpApi<IProjectInfoApi>().ConfigureHttpApiConfig(config); //注入接口

            return services;
        }
    }

    /// <summary>
    /// 基于DependencyInjection的扩展
    /// </summary>
    public static class DependencyInjectionExtensions
    {
        /// <summary>
        /// 添加HttpApi
        /// 返回HttpApi工厂
        /// </summary>
        /// <typeparam name="TInterface">接口类型</typeparam>
        /// <param name="services"></param>
        /// <returns></returns>
        public static HttpApiFactoryBuilder<TInterface> AddHttpApi<TInterface>(this IServiceCollection services)
            where TInterface : class, IHttpApi
        {
            return new HttpApiFactoryBuilder<TInterface>(services);
        }
    }
}
